package com.hainiu.cat.web.lintcode;

/**
 * create by biji.zhao on 2021/6/8
 */
public class Solution {

    private static double check(double x, int[][] barracks) {
        double maxx = 0;
        int n = barracks.length;
        for (int i = 0; i < n; i++) {
            double tmp = Math.sqrt(barracks[i][1] * barracks[i][1] + (barracks[i][0] - x) * (barracks[i][0] - x));
            maxx = Math.max(maxx, tmp);
        }
        return maxx;
    }

    public static double fetchSuppliesII(int[][] barracks) {
        // write your code here
        double l = -10000, r = 10000;
        double mid = 0;
        // 循环一百次，直接推出
        for (int i = 0; i < 100; i++) {
            mid = l + (r - l) / 2;
            double midmid = mid + (r - mid) / 2;
            if (check(mid, barracks) > check(midmid, barracks)) {
                l = mid;
            } else {
                r = midmid;
            }
        }
        return check(mid, barracks);
        // l--------------------mid---------midmid---------r
    }

    /**
     * @param costPerCut: 表示每次切割的成本
     * @param salePrice: 承包商同意支付的每单位长度价格
     * @param lengths: 一堆钢材
     * @return: 最大利润
     */
    public int maxProfit(int costPerCut, int salePrice, int[] lengths) {
        // 每根管子长度(0~10000]
        // 切割成本和单价在(0~1000]
        // 管子数量[1, 50]
        // write your code here
        for (int i = 0; i < 1000; i++) {

        }

        // totalProfit = totalUniformRods（是可销售的金属棒数量） * saleLength * salePrice - totalCuts * costPerCut

        // 不确定的totalUniformRods、saleLength、totalCuts

        // totalProfit = totalUniformRods（是可销售的金属棒数量） * saleLength * 5 - totalCuts * 1

//        for (int i = 0; i < 50; i++) {
//            // 切割有成本 所以不能无限小
//
//            // 长度必须一致
//        }
        return 1;
    }

    public static void main(String[] args) {
        int [][] input = {{-3303,3448}, {-6163,6529}, {-7256,9636}, {-8539,-8208}, {437,-5603}, {8071,1851}};
        double v = fetchSuppliesII(input);
        System.out.println(v);
    }

}
